import { useRef, useState, useMemo } from 'react';

export const useFormData = (initData: { [key: string]: string } = {}) => {
  const formData = useRef(initData);
  const [, update] = useState(false);

  const handleForm = useMemo(() => {
    const setFormItem = (key: string, value: any) => {
      formData.current[key] = value;
      update(v => !v);
    };
    const resetForm = () => {
      Object.keys(formData.current).forEach(key => (formData.current[key] = ''));
      update(v => !v);
    };
    return [setFormItem, resetForm] as const;
  }, []);
  return [formData.current, ...handleForm] as const;
};
